/*****************************************************************************************
  Project   : The Impact of Unions on Non-union Wage Setting: Threats and Bargaining
  Authors   : David A. Green, Ben M. Sand, Iain G. Snoddy, Jeanne Tschopp
  Date      : August 2025
  Output    : Table 3 -  Decomposition
*****************************************************************************************/



clear
set more off
cap log close

// Table 3 - Decomposition


cap mat drop _all

cap program drop tbl2
program define tbl2 
syntax, from(int) to(int) grp(string) 

	use "${wd}/decomp_grp_`grp'_case_6.dta", clear 
	
	keep if inlist(year, `from',`to')
	drop observed  part1 part2 deunion cf2 cf1 cf3 cf_* 
	
	// Overall observed wage trends
	gen observed = W_obs0 * (1 - union_share  ) + W_obs1 * union_share - (W_obs0[1] * (1 - union_share[1]) + W_obs1[1] * union_share[1])

	// part due to changing union proportion
	gen part1   = (union_share - union_share[1]) * (W_obs1 - W_obs0)

	// part due to changing wage differentials
	gen part2   = union_share[1] * (W_obs1 - W_obs0 - (W_obs1[1] - W_obs0[1]))

	// CF 2 - non-union wage trends (ie, holdign union proportion and differential constant)
	gen cf2   = W_obs0 - W_obs0[1] 

	// CF 1 - holding union proportion constant.
	gen cf1 = part2 + cf2

	// Decomposing CF 2 into de-union effect and CF 3	

	// Deunion	
	gen deunion  =  W_obs0 - W_full0

	// CF 3 - full counterfactual
	gen cf3      =  W_full0 - W_obs0[1]

	// full decomp of compsition effect

	gen cf_threat = W_obs0 - W_second0
	gen cf_transition = W_second0 -  W_first0
	gen cf_premia =  W_first0 - W_full0

	keep observed  part1 part2 deunion cf_* 
	order observed part1 part2 deunion  cf_threat cf_transition cf_premia  

	keep if _n==2
	
	mkmat *, matrix(`grp')
	mat `grp' =  `grp''
	mat colnames `grp' = "`grp'"
	
	mata {
		A = st_matrix("`grp'")
		A
		B = sum(A[2..4])
		B
		C = B / A[1] 
		C
		D = A \ B \ C
		st_matrix("`grp'", D)
		}
		
	mat colnames `grp' = "`grp'"
end


// entire period
tbl2, grp(all) from(1) to(4) 
tbl2, grp(men) from(1) to(4) 
tbl2, grp(women) from(1) to(4) 
tbl2, grp(EF_young_low) from(1) to(4) 
tbl2, grp(EF_young_high) from(1) to(4) 
tbl2, grp(EF_old_low) from(1) to(4) 
tbl2, grp(EF_old_high) from(1) to(4) 


mat res = all, men, women, EF_young_low, EF_young_high, EF_old_low, EF_old_high

clear
svmat res, names(col)


renvars ,postfix(_n)

foreach var in all men women EF_young_low  EF_young_high EF_old_low EF_old_high { 
	gen str `var' = string(`var'_n,"%4.3f")
}
drop *_n

gen str item = ""
local i = 1
foreach var in "(1) Observed" "(2) Union Prop." "(3) Union Premium"  "(4) Non-Union Spillovers" " \ \ (4a) Fixed Threat " " \ \ (4b) Fixed Transitions" " \ \ (4c) Fixed Union Prem. "  "(5) Total " "(6) Total/Observed" {
	replace item = "`var'" if _n == `i'
	local ++i
}
order  item

texsave using "${tables}//Table_3.tex",  replace frag autonumber nofix width(15cm)  rowsep(.1cm) varlabels location("ht")    ///
label(tab:decompfull) ///
headerlines("&  All & Men & Women & \multicolumn{4}{c}{Men} " "\cmidrule{5-8} " "\addlinespace[-5ex]" "& & & & \footnotesize{Young Low Skill} & Young High Skill & Old Low Skill & Old High Skill " "\cmidrule{2-8} "  "\addlinespace[-5ex]"  "& \multicolumn{7}{c}{1980-2010} " "\addlinespace[-5ex]" ) hlines(3 7 8) size(footnotesize) ///
 footnote(\textbf{\textsc{Notes:}} This table displays results from the decomposition for union and nonunion workers from 1980-2010.  Each column contains the decomposition results for a different subsample. All figures are log changes from 1980 levels.  Details described in main text.    ,width(15cm)   ) ///
 title(Outside Options Contribution to Changing Wages - Subsample Analysis) 
 
 
 
/*
// Just 1980s
tbl2, grp(all) from(1) to(2) 
tbl2, grp(men) from(1) to(2) 
tbl2, grp(women) from(1) to(2) 
tbl2, grp(EF_young_low) from(1) to(2) 
tbl2, grp(EF_young_high) from(1) to(2) 
tbl2, grp(EF_old_low) from(1) to(2) 
tbl2, grp(EF_old_high) from(1) to(2)

mat res = all, men, women, EF_young_low, EF_young_high, EF_old_low, EF_old_high

clear
svmat res, names(col)

renvars ,postfix(_n)

foreach var in all men women EF_young_low  EF_young_high EF_old_low EF_old_high {  
	gen str `var' = string(`var'_n,"%4.3f")
}
drop *_n

gen str item = ""
local i = 1
foreach var in "(1) Observed" "(2) Union Prop." "(3) Union Premium"  "(4) Non-Union Spillovers" " \ \ (4a) Fixed Threat " " \ \ (4b) Fixed Transitions" " \ \ (4c) Fixed Union Prem. "  "(5) Total " "(6) Total/Observed" {
	replace item = "`var'" if _n == `i'
	local ++i
}
order  item

// Not shown in paper
texsave using "${tables}//Table_3_1980s.tex",  replace frag autonumber nofix width(15cm)  rowsep(.1cm) varlabels location("h")    ///
headerlines("&  All & Men & Women & Age 36-55 & Age 20-26 & $ \leq $ HS & $ > $ HS " "\cmidrule{2-8} " "\addlinespace[-5ex]" "& \multicolumn{7}{c}{1980-1990} " "\addlinespace[-5ex]" ) hlines(3 7 8) ///
 footnote(\textbf{\textsc{Notes:}} This table displays results from the decomposition for union and nonunion workers from 1980-2010.  Each column contains the decomposition results for a different subsample. All figures are log changes from 1980 levels.  Details described in main text.    ,width(15cm)   ) ///
 title(Outside Options Contribution to Changing Wages - Subsample Analysis)

	
*/	
	
	
	
	 
